package com.tpyule.tpclass.service.impl;

import com.tpyule.tpclass.dao.StudyDao;
import com.tpyule.tpclass.entity.bean.SubscriptionAction;
import com.tpyule.tpclass.entity.bo.AnaBo;
import com.tpyule.tpclass.entity.bo.CourseLearnDetailBo;
import com.tpyule.tpclass.entity.bo.StudyCountBo;
import com.tpyule.tpclass.entity.bo.StudyDetailBo;
import com.tpyule.tpclass.entity.dto.CourseStudyCountDto;
import com.tpyule.tpclass.entity.dto.StudyAnaDto;
import com.tpyule.tpclass.entity.dto.StudyProDto;
import com.tpyule.tpclass.entity.dto.StudyQueryDto;
import com.tpyule.tpclass.entity.pojo.Section;
import com.tpyule.tpclass.entity.pojo.Studied;
import com.tpyule.tpclass.entity.pojo.Study;
import com.tpyule.tpclass.entity.pojo.Subscription;
import com.tpyule.tpclass.service.SectionService;
import com.tpyule.tpclass.service.StudiedService;
import com.tpyule.tpclass.service.StudyService;
import com.tpyule.tpclass.service.SubscriptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author zzz
 * @version 1.0
 * @date 2020/4/14 0:00
 */
@Service
public class StudyServiceImpl implements StudyService {
    @Autowired
    StudyDao studyDao;
    @Autowired
    StudiedService studiedService;
    @Autowired
    SectionService sectionService;
    @Autowired
    SubscriptionService subscriptionService;

    @Override
    public StudyDetailBo getStudyDetail(StudyQueryDto studyQueryDto) {
        StudyDetailBo studyDetailBo = null;
        Study study = studyDao.getStudy(studyQueryDto);
        // 表示该课程已经开始学习
        if (study != null) {
            studyDetailBo = new StudyDetailBo();
            studyDetailBo.setCourseId(study.getCourseId());
            studyDetailBo.setUserId(study.getUserId());
            studyDetailBo.setStudyTime(study.getStudyTime());
            Section section = studyDao.getLastStudy(studyQueryDto);
            // 表示为没学习完
            if (section != null) {
                studyDetailBo.setLearn(false);
                studyDetailBo.setLastSectionId(section.getSectionId());
                studyDetailBo.setSectionName(section.getName());
            } else {
                studyDetailBo.setLearn(true);
            }
            studyDetailBo.setScount(studyDao.getStudiedSize(studyQueryDto));
            studyDetailBo.setTotal(sectionService.getSectionSize(studyQueryDto.getCourseId()));
        }
        return studyDetailBo;
    }

    @Override
    public int addStudy(Study study, String sectionId) {
        int result = -1;
        int sedResult = -1;
        if (study != null) {
            study.setStudyId(UUID.randomUUID().toString().toUpperCase());
            study.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            result = studyDao.addStudy(study);
            Section section = sectionService.getIndexSection(study.getCourseId());
            String newSectionId = section.getSectionId();
            Studied studied = new Studied();
            studied.setCourseId(study.getCourseId());
            studied.setSectionId(sectionId == null ? newSectionId : sectionId);
            studied.setUserId(study.getUserId());
            sedResult = studiedService.addStudied(studied);
            while(sedResult<=0){
                sedResult = studiedService.addStudied(studied);
            }
            Subscription subscription = new Subscription();
            subscription.setSubscriptionId(UUID.randomUUID().toString().toUpperCase());
            subscription.setTargetId(study.getCourseId());
            subscription.setTargetType("course");
            subscription.setAction(SubscriptionAction.update);
            subscription.setUserId(study.getUserId());
            subscription.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            subscriptionService.addSubscription(subscription);
        }
        return result;
    }

    @Override
    public List<CourseLearnDetailBo> getListStudyCourse(String userId) {
        List<CourseLearnDetailBo> bos = studyDao.getListStudyCourse(userId);
        if (bos!=null) {
            for (int i = 0; i < bos.size(); i++) {
                bos.get(i).setSectionSize(sectionService.getSectionSize(bos.get(i).getCourseId()));
            }
        }

        return bos;
    }

    @Override
    public int getCountStudy(CourseStudyCountDto dto) {
        return studyDao.getCountStudy(dto);
    }

    @Override
    public int getLastCountStudy(CourseStudyCountDto dto) {
        return studyDao.getLastCountStudy(dto);
    }

    @Override
    public List<AnaBo> getAnaStudy(StudyAnaDto dto) {
        return studyDao.getAnaStudy(dto);
    }

    @Override
    public List<StudyCountBo> getCountSexStudy(String courseId) {

     // gender 用户性别：0：保密1：男2：女
        List<StudyCountBo> bos = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            StudyCountBo bo = new StudyCountBo();
            if (i==0){
                bo.setName("未知");
                bo.setValue(studyDao.getCountSexStudy(courseId, i+""));
            }else if (i==1){
                bo.setName("男");
                bo.setValue(studyDao.getCountSexStudy(courseId, i+""));
            }else if (i==2){
                bo.setName("女");
                bo.setValue(studyDao.getCountSexStudy(courseId, i+""));
            }
            bos.add(bo);
        }
        return bos;
    }

    @Override
    public List<StudyCountBo> getCountProgressStudy(StudyProDto dto) {
        int sectionSize = sectionService.getSectionSize(dto.getCourseId());
        int two =(int)Math.floor( sectionSize * 0.2);
        int eight = (int)Math.floor( sectionSize * 0.8);
        // gender 用户性别：0：保密1：男2：女
        List<StudyCountBo> bos = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            StudyCountBo bo = new StudyCountBo();
            if (i==0){
                bo.setName("20%以下");
                dto.setMax(two+"");
                bo.setValue(studyDao.getCountDetailStudy(dto));
            }else if (i==1){
                bo.setName("20%-80%");
                dto.setMin(two+"");
                dto.setMax(eight+"");
                bo.setValue(studyDao.getCountDetailStudy(dto));
            }else if (i==2){
                bo.setName("80%以上");
                dto.setMax(eight+"");
                bo.setValue(studyDao.getCountDetailStudy(dto));
            }
            bos.add(bo);
        }
        return bos;
    }
}
